﻿@{
    ViewBag.Title = "Good";
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
}
<script src="/Scripts/ajaxfileupload.js"></script>
<input name="id" id="id" type="hidden" value="0" />
<table style="font-size: 12px;margin-left:10px;">
    <tr>
        <td colspan="2">
            <img src="" width="320" height="165" alt="640px   330px" onclick="checkfile()" id="img" />
            <input id="fileToUpload" style="display: none; cursor: pointer;" type="file" onchange="upload()" name="upfile">
        </td>
    </tr>
   
    <tr>
        <td><label>跳转链接</label></td>
        <td><input type="text" id="url" name="sort" style="width: 300px;" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="botton" style="margin-left: 50px;" onclick="edit()">保存</div>
            <div class="botton" style="margin-left: 50px;" onclick="javascript: history.go(-1);">取消</div>
        </td>      
    </tr>
</table>

<div style="width: 1000px; height: 40px; margin-top: 200px; "></div>

<script>
    var id = "@ViewData["id"]";
    if (parseFloat(id) > 0) {
        $.post("@Url.Action("GetOneData")", { id: id }, function(data) {
            var obj = JSON.parse(data);
            $("#id").val(obj[0].Id);
            $("#img").attr("src", obj[0].Img);
            $("#url").val(obj[0].Url);
        });
    }

    function edit() {
       
        var img = $("#img").attr("src");
        if (img == "") {
            alert("请上传图片！");
            return false;
        }
        var url = $("#url").val();
        url = url.replace(/\s/g, "");
        if (url.length > 100) {
            alert("链接长度不能大于100个字符！");
            return false;
        }
        $.post("@Url.Action("Edit")", { id: $("#id").val(), img: img,url:url }, function(row) {
            if (row > 0) {
                alert("操作成功！");

            } else {
                alert("添加失败！");
            }
            location.href = "@Url.Action("Index")";

        });
    }
    function checkfile() {
        $('#fileToUpload').click();
    }
    function upload() {
        var val = $("#fileToUpload").val();
        if (val == "") {
            alert("请上传文件");
            return false;
        }
        var suffix = val.split('.');
        var arr = new Array("png", "jpg", "gif");
        if (arr.indexOf(suffix[suffix.length - 1]) < 0) {
            alert("上传格式有误！");
        } else {
            $.ajaxFileUpload({
                url: '@Url.Action("UploadImg")',
                secureuri: false,
                fileElementId: 'fileToUpload',
                dataType: 'json',
                success: function (data, status) {
                    var str = "";
                    if (data.status == 1) {
                        $("#img").attr("src", data.path);       
                    } else {

                    }

                }
            });
        }
    }
</script>


